<?php

class CategoriesController extends Controller
{
    /**
     * Declares class-based actions.
     */
    public $categories;
    public $pages;
    public $breadcrumbs;
    /**
     * This is the default 'index' action that is invoked
     * when an action is not explicitly requested by users.
     */
    public function findCategoryName($id, $categories)
    {
        foreach ($categories as $category) {
            if ($category->id === $id)
            {
                return $category->name;
            }
        }
        return "";
    }

    public function actionIndex()
    {
 
        $criteria = new CDbCriteria();
        $criteria->select = 'id, name';
        $criteria->condition = 'status = :status';
        $criteria->params = array(':status'=> 1);
        $this->categories = Category::model()->findAll($criteria);

        $id = isset($_GET["id"]) ? $_GET["id"] : $this->categories[0]->id;
        $categoryName = $this->findCategoryName($id, $this->categories);

        $criteria = new CDbCriteria();
        $criteria->select = 'id, name, on_sale, image';
        $criteria->condition = 'category_id = :id';
        $criteria->order = 'on_sale ASC';
        $criteria->params = array(':id'=> $id);

        $count= Product::model()->count($criteria);
        $this->pages= new CPagination($count);
        $this->pages->pageSize = 9;
        $this->pages->applyLimit($criteria);

        $products = Product::model()->findAll($criteria);
        
        $this->breadcrumbs = array( array('name' => Yii::t("view_partials_header","Products"), 'url' => $this->createUrl('categories/index')),
                                    array('name' => $categoryName, 'url' => $this->createUrl('categories/index', array('id'=>$id))),
                             );
        $this->layout = "ProductLayout";
        $this->render("Index", array("products" => $products));
    }

    public function actionShow($id='',$layout='')
    {
        if($id){
			$criteria = new CDbCriteria();
			$criteria->select = 'id, name';
			$criteria->condition = 'status = :status';
			$criteria->params = array(':status'=> 1);
			$this->categories = Category::model()->findAll($criteria);


			$criteria = new CDbCriteria();
			$criteria->select = 'id, name, on_sale, image, code, packing, last_update, description, category_id';
			$criteria->condition = 'status = :status and id = :id';
			$criteria->params = array(':status'=> 1, ':id' => $id);
			$product = Product::model()->find($criteria);
		}
        else
			$product = null;
        if($product === null)
            throw new CHttpException(404,'The requested page does not exist.');

        //Set layout show-product view
        if($layout){
            $this->layout = 'ProductPopupLayout';
        }else{
            $this->layout = 'ProductLayout';
        }

        $categoryName = $this->findCategoryName($product->category_id, $this->categories);
        $this->breadcrumbs = array( array('name' => Yii::t("view_partials_header","Products"), 'url' => $this->createUrl('categories/index')),
                                    array('name' => $categoryName, 'url' => $this->createUrl('categories/index', array('id'=> $product->category_id))),
                                    array('name' => $product->name, 'url' => $this->createUrl('categories/show', array('id'=> $product->id))),
                             );

        $this->render("Show", array("product" => $product));
        
    }

}